package com.yxq.mdt.app.modules.socialcontact.ui.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ToastUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.yxq.mdt.app.R;
import com.yxq.mdt.app.base.BaseFragment;
import com.yxq.mdt.app.modules.bottomsheet.CommentBottomSheetDialog;
import com.yxq.mdt.app.modules.personal.adapter.CommentReplyListAdapter;
import com.yxq.mdt.app.modules.personal.bean.MainPersonalStateBean;
import com.yxq.mdt.app.modules.socialcontact.bean.BrowseListBean;
import com.yxq.mdt.app.modules.socialcontact.bean.GroupTopicListBean;
import com.yxq.mdt.app.modules.socialcontact.bean.SocialStateGiveListLikeBean;
import com.yxq.mdt.app.modules.socialcontact.model.MainSocialContactModel;
import com.yxq.mdt.app.modules.socialcontact.presenter.MainSocialPageInfoContract;
import com.yxq.mdt.app.modules.socialcontact.presenter.MainSocialPgaeInfoPresenter;
import com.yxq.mdt.app.modules.socialcontact.ui.activity.SocialPersonalHomepageAc;
import com.yxq.mdt.app.modules.socialcontact.ui.activity.SocialStateDetailsAc;
import com.yxq.mdt.app.net.ApiModel;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.jzvd.Jzvd;

/**
 * 动态里面的评论
 */
public class SocialStateCommentFragment extends BaseFragment<MainSocialPgaeInfoPresenter> implements MainSocialPageInfoContract.View {
    public static final String EXTRA_TEXT = "extra_text";
    @BindView(R.id.social_state_recyclerView)
    RecyclerView socialStateRecyclerView;
    CommentReplyListAdapter mainSocialContactAdapter;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    private List<MainPersonalStateBean.ListBean> mainSocialContactBeans;
    CommentBottomSheetDialog commentBottomSheetDialog;
    private String topicId = "", give = "", ReplyId;
    FragmentManager comment;
    SocialStateDetailsAc.OnNumberChangeListener onNumberChangeListener;

    public SocialStateCommentFragment(SocialStateDetailsAc.OnNumberChangeListener onNumberChangeListener) {
        this.onNumberChangeListener = onNumberChangeListener;
    }

    @Override
    protected int attachLayoutId() {
        setImmersionBarEnabled(false);
        return R.layout.fragment_social_state_comment;
    }

    @Override
    protected MainSocialPgaeInfoPresenter getPresenter() {
        return new MainSocialPgaeInfoPresenter(this, new MainSocialContactModel());
    }

    @Override
    public void initView(View view) {
        Bundle bundle = getArguments();
        if (bundle != null) {
            topicId = bundle.getString("topicId");
            give = bundle.getString("give");
        }
        mainSocialContactBeans = new ArrayList<>();
        socialStateRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        mainSocialContactAdapter = new CommentReplyListAdapter(R.layout.item_comment_layout, mainSocialContactBeans, getChildFragmentManager(), this);
        socialStateRecyclerView.setAdapter(mainSocialContactAdapter);
        mainSocialContactAdapter.setOnItemClickListener((adapter, view1, position) -> {
            Intent it = new Intent(mContext, SocialPersonalHomepageAc.class);
            it.putExtra("userId", mainSocialContactAdapter.getItem(position).getUserId() + "");
            startActivity(it);
        });

        mainSocialContactAdapter.setOnItemChildClickListener((adapter, view1, position) -> {
            switch (view1.getId()) {
                case R.id.item_layout_reply:
                    ReplyId = mainSocialContactAdapter.getItem(position).getId();
                    commentBottomSheetDialog = new CommentBottomSheetDialog(SocialStateCommentFragment.this, topicId, ReplyId, 1);
                    commentBottomSheetDialog.show(comment, "commentBottomSheetDialog");
                    break;
                case R.id.give_like_lint:
                    int giveNum = mainSocialContactAdapter.getItem(position).getGiveNum();
                    if (mainSocialContactAdapter.getItem(position).getGiveLike() != 0) {
                        MainSocialContactModel.delGsTopicFavour("4", mainSocialContactAdapter.getItem(position).getId(), new ApiModel.CommonCallback<String>() {
                            @Override
                            public void onSuccess(String s) {
                                mainSocialContactAdapter.getItem(position).setGiveLike(0);
                                mainSocialContactAdapter.getItem(position).setGiveNum(giveNum - 1);
                                mainSocialContactAdapter.notifyDataSetChanged();
                                ToastUtils.showLong("取消点赞成功");
                            }

                            @Override
                            public void onError(String errMsg) {
                                ToastUtils.showLong("取消点赞失败");
                            }
                        });
                    } else {
                        MainSocialContactModel.saveGsTopicFavour(mainSocialContactAdapter.getItem(position).getId(), "4", new ApiModel.CommonCallback<String>() {
                            @Override
                            public void onSuccess(String s) {
                                mainSocialContactAdapter.getItem(position).setGiveLike(1);
                                mainSocialContactAdapter.getItem(position).setGiveNum(giveNum + 1);
                                mainSocialContactAdapter.notifyDataSetChanged();
                                ToastUtils.showLong("点赞成功");
                            }

                            @Override
                            public void onError(String errMsg) {
                                ToastUtils.showLong("点赞失败");
                            }
                        });
                    }
                    break;
            }
        });
        mainSocialContactAdapter.setEmptyView(R.layout.layout_empty_view, socialStateRecyclerView);
        mainSocialContactAdapter.isUseEmpty(false);
        refreshLayout.setOnRefreshListener(refreshLayout -> {
            page = 1;
            mPresenter.getPageInfoList(String.valueOf(page), String.valueOf(30), topicId);
        });
        refreshLayout.setOnLoadMoreListener(refreshLayout -> {
            page++;
            mPresenter.getPageInfoList(String.valueOf(page), String.valueOf(30), topicId);
        });
        comment = getChildFragmentManager();
    }

    @Override
    public void initData() {
        refreshLayout.autoRefresh();
    }


    @Override
    public void setData(@Nullable Object data) {

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                getActivity().finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Jzvd.releaseAllVideos();
    }


    @Override
    public void onLoadPageInfoSuccess(MainPersonalStateBean mainPersonalStateBean) {
        refreshLayout.finishLoadMore();
        refreshLayout.finishRefresh();

        if (page == 1) {
            mainSocialContactAdapter.setNewData(mainPersonalStateBean.getList());
        } else {
            if (mainPersonalStateBean.getList() != null && mainPersonalStateBean.getList().size() != 0) {
                mainSocialContactAdapter.addData(mainPersonalStateBean.getList());
                mainSocialContactAdapter.notifyDataSetChanged();
            } else {
                refreshLayout.finishLoadMoreWithNoMoreData();
            }
        }
        mainSocialContactAdapter.isUseEmpty(!(mainSocialContactAdapter.getData().size() > 0));
        if (onNumberChangeListener != null) {
            onNumberChangeListener.onChange(mainPersonalStateBean.getTotal(), -1);
        }
    }

    @Override
    public void onLoadPageInfoFailed() {
        refreshLayout.finishLoadMore();
        refreshLayout.finishRefresh();
    }

    @Override
    public void onLoadTopicByIdSuccess(GroupTopicListBean groupTopicListBean) {

    }

    @Override
    public void onLoadTopicByIdFailed() {

    }

    @Override
    public void onLoadGiveFailed() {

    }

    @Override
    public void onLoadGiveSuccess(SocialStateGiveListLikeBean socialStateGiveListLikeBean) {

    }

    @Override
    public void onLoadBrowseListFailed() {

    }

    @Override
    public void onLoadBrowseListSuccess(BrowseListBean socialStateGiveListLikeBean) {

    }


    @Override
    public void onLoadTopicFavorSuccess(String s) {
    }

    @Override
    public void onLoadTopicFavorFailed() {

    }


}
